/*
There are two sorted arrays nums1 and nums2 of size m and n respectively.

Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

You may assume nums1 and nums2 cannot be both empty.

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0
Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/median-of-two-sorted-arrays
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
*/

#include <vector>

using namespace std;

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        vector<int> nums3;

		int size1 = nums1.size();
		int size2 = nums2.size();

		int i = 0;
		int j = 0;

		while (i < size1 && j < size2)
		{
			if (nums1[i] < nums2[j])
			{
				nums3.push_back(nums1[i]);
				i++;
			}
			else
			{
				nums3.push_back(nums2[j]);
				j++;
			}
		}

		while (i < size1)
		{
			nums3.push_back(nums1[i]);
			i++;
		}
		
		while (j < size2)
		{
			nums3.push_back(nums2[j]);
			j++;
		}

        double median = 0;
        int length = nums3.size();

        if(length % 2 == 0)
        {
            median = ((double)nums3[length/2] + (double)nums3[length/2 - 1]) / 2;
        }
        else
        {
            median = nums3[(length-1)/2];
        }

        return median;
    }
};
